// auction/actionInfo/actionInfo.js
import http from '../../http'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    auctionList: [],
    resultBol: false,
    auctionId: '',
    page: 0, // 当前页码
    limit: 10, // 每页数量
    isReturn: false,
    loading: false,
    status:0
  },
  gain() {
    this.setData({
      page: this.data.page + 1,
      loading: true
    }, () => {
      http.post({
        url: '/Auction/Detail',
        data: {
          id: this.data.auctionId,
          user_id: wx.getStorageSync('userId'),
          page: this.data.page,
          limit: this.data.limit,
        },
        then: (res) => {
          console.log('拍卖会详情', res);
          let info = res.data.data
          info.startTime = info.start_time_text.slice(5)
          info.endTime = info.end_time_text.slice(5)
          if (this.data.auctionList.length >= info.items_count) {
            this.setData({
              loading: false,
            })
          } else {
            this.setData({
              info: info,
              auctionList: [...this.data.auctionList, ...info.items],
              isReturn: true,
              loading: false
            })
          }
        }
      })
    })
  },
  onTabChange(event) {
    console.log(event.detail.value);
    this.setData({
      status: event.detail.value
    }, () => {
      // this.gain()
    })
  },
  bottomInt() {
    if (!this.data.isReturn) {
      return
    }
    this.setData({
      isReturn: false
    }, () => {
      this.gain()
    })
  },
  goodsInfo(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/auction/goodsInfo/goodsInfo?id=${id}`
    });
  },
  // 收藏
  collect() {
    if (this.data.info.collect) {
      http.post({
        url: '/Collect/Delete',
        data: {
          id: this.data.info.id,
          user_id: wx.getStorageSync('userId'),
        },
        then: (res) => {
          console.log('取消拍卖会收藏', res);
          if (res.data.code == 1) {
            wx.showToast({
              title: '已取消',
              icon: 'success',
              duration: 2000
            })
            this.setData({
              'info.collect': !this.data.info.collect
            })
          }
        }
      })
    } else {
      http.post({
        url: '/Collect/Add',
        data: {
          user_id: wx.getStorageSync('userId'),
          id: this.data.info.id
        },
        then: (res) => {
          console.log('添加拍卖会收藏', res);
          if (res.data.code == 1) {
            wx.showToast({
              title: '已收藏',
              icon: 'success',
              duration: 2000
            })
            this.setData({
              'info.collect': !this.data.info.collect
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'error',
            })
          }
        }
      })
    }
  },
  result() {
    this.setData({
      resultBol: true
    })
  },
  cancel() {
    this.setData({
      resultBol: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = getCurrentPages()[getCurrentPages().length - 1].options
    this.setData({
      auctionId: options.id
    }, () => {
      this.gain()
    })
    http.post({
      url: '/Auction/View',
      data: {
        id: options.id,
        user_id: wx.getStorageSync('userId'),
      },
      then: (res) => {
        console.log('增加浏览记录', res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})